<?php
add_action( 'widgets_init', 'bugu_load_widgets' );

function bugu_load_widgets() {
    register_widget( 'Bugu_TagCloud_Widget' );
}

class Bugu_TagCloud_Widget extends WP_Widget {
    /**
     * Widget setup.
     */
    function __construct() {
        /* Widget settings. */
        $widget_ops = array( 'classname' => 'bugu_tagCloud', 'description' => '可配置副标题的标签云' );
        /* Widget control settings. */
        $control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'bugu-tagCloud-widget' );
        /* Create the widget. */
        parent::__construct( 'tagCloud-widget', '布谷标签云', $widget_ops, $control_ops );
    }
    /**
     * How to display the widget on the screen.
     */
    function widget( $args, $instance ) {
        extract( $args );
        /* Our variables from the widget settings. */
        $title = apply_filters('widget_title', $instance['title'] );
        $subtitle = $instance['subtitle'];
        echo $before_widget;
        if ( $title )
            echo $before_title . $title . '<small>' . $subtitle . '</small>' . $after_title;
        wp_tag_cloud( array(
            'number' => 30,
            'smallest' => 11,
            'largest' => 30,
            'unit' => 'px',
        ) );
        echo $after_widget;
    }
    /**
     * Update the widget settings.
     */
    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = strip_tags( $new_instance['title'] );
        $instance['subtitle'] = strip_tags( $new_instance['subtitle'] );
        return $instance;
    }

    function form( $instance ) {
        $defaults = array( 'title' => '', 'subtitle' => '' );
        $instance = wp_parse_args( (array) $instance, $defaults ); ?>
        <p>
            <label for="<?php echo $this->get_field_id( 'title' ); ?>">标题</label>
            <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:100%;" />
        </p>
        <!-- Your Name: Text Input -->
        <p>
            <label for="<?php echo $this->get_field_id( 'subtitle' ); ?>">副标题</label>
            <input id="<?php echo $this->get_field_id( 'subtitle' ); ?>" name="<?php echo $this->get_field_name( 'subtitle' ); ?>" value="<?php echo $instance['subtitle']; ?>" style="width:100%;" />
        </p>
    <?php
    }
}
?>
